package com.weilive.core.netty.http;

import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;

import java.nio.charset.Charset;

import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.weilive.core.utils.QueryStringDecoder;
import com.weilive.core.utils.StringUtils;

public abstract class HttpServlet {

	protected static final Logger log = LoggerFactory.getLogger(HttpServlet.class);

	/**
	 * 处理request
	 * @param request 请求
	 * @return 返回response，如果为null，则表示被代理，由别人来返回response
	 */
	public abstract HttpResponse processRequest(HttpRequest request) throws Exception;
	
	/**
	 * 获取servlet的root路径，必须以“/”开头 
	 */
    public String getServletPath() {
        return "/" + this.getClass().getSimpleName();
    }
    
    /**
     * 解析QueryString
     */
    public static QueryStringDecoder decodeQueryString(HttpRequest request) throws Exception {
    	if (request.getMethod() == HttpMethod.GET) {
    		return new QueryStringDecoder(request.getUri());
    	} else {
    		String uri = request.getUri();
    		int pos = uri.indexOf('?');
    		if (pos < 0) {
    			return new QueryStringDecoder(uri, getRequestContent(request));
    		} else if (uri.endsWith("&")){
    			return new QueryStringDecoder(uri.substring(0, pos), uri.substring(pos + 1) + getRequestContent(request));
    		} else {
    			return new QueryStringDecoder(uri.substring(0, pos), uri.substring(pos + 1) + "&" + getRequestContent(request));
    		}
    	}
    }
    
    /**
     * 获取request的内容 
     */
    public static String getRequestContent(HttpRequest request) {
    	return getRequestContent(request, StringUtils.DEFAULT_CHARSET);
    }
    
    public static String getRequestContent(HttpRequest request, Charset charset) {
    	return request.getContent().toString(charset);
    }
    
    /**
     * 创建json response 
     */
    public static HttpResponse createJsonResponse() {
    	return createJsonResponse(null, HttpResponseStatus.OK, null, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createJsonResponse(String content) {
    	return createJsonResponse(null, HttpResponseStatus.OK, content, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createJsonResponse(String content, Charset charset) {
    	return createJsonResponse(null, HttpResponseStatus.OK, content, charset);
    }
    
    public static HttpResponse createJsonResponse(HttpRequest request, HttpResponseStatus status) {
    	return createJsonResponse(request, status, null, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createJsonResponse(HttpRequest request, String content) {
    	return createJsonResponse(request, HttpResponseStatus.OK, content, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createJsonResponse(HttpRequest request, String content, Charset charset) {
    	return createJsonResponse(request, HttpResponseStatus.OK, content, charset);
    }
    
    public static HttpResponse createJsonResponse(HttpRequest request, HttpResponseStatus status, String content) {
    	return createJsonResponse(request, status, content, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createJsonResponse(HttpRequest request, HttpResponseStatus status, String content, Charset charset) {
		return createResponse(request, status, content, charset, "application/json");
    }
    
    /**
     * 创建xml response 
     */
    public static HttpResponse createXmlResponse() {
    	return createXmlResponse(null, HttpResponseStatus.OK, null, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createXmlResponse(String content) {
    	return createXmlResponse(null, HttpResponseStatus.OK, content, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createXmlResponse(String content, Charset charset) {
    	return createXmlResponse(null, HttpResponseStatus.OK, content, charset);
    }
    
    public static HttpResponse createXmlResponse(HttpRequest request, HttpResponseStatus status) {
    	return createXmlResponse(request, status, null, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createXmlResponse(HttpRequest request, String content) {
    	return createXmlResponse(request, HttpResponseStatus.OK, content, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createXmlResponse(HttpRequest request, String content, Charset charset) {
    	return createXmlResponse(request, HttpResponseStatus.OK, content, charset);
    }
    
    public static HttpResponse createXmlResponse(HttpRequest request, HttpResponseStatus status, String content) {
    	return createXmlResponse(request, status, content, StringUtils.DEFAULT_CHARSET);
    }
    
    public static HttpResponse createXmlResponse(HttpRequest request, HttpResponseStatus status, String content, Charset charset) {
		return createResponse(request, status, content, charset, "text/html");
    }
    
    /**
     * 
     * @param request
     * @param status
     * @param content
     * @param charset
     * @param type
     * @return
     */
    
    public static HttpResponse createResponse(HttpRequest request, HttpResponseStatus status, String content, Charset charset, String type) {
    	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
//    	response.setHeader(HttpHeaders.Names.DATE, StringUtils.defFormatDate(new Date()));
//		response.setHeader(HttpHeaders.Names.SERVER, "bigbear-server");
		response.setHeader(HttpHeaders.Names.CONTENT_TYPE, String.format("%s; charset=%s", type, charset.name()));
		if (content != null) {
			response.setContent(ChannelBuffers.copiedBuffer(content, charset));
		}
        return response;
    }
}
